package cn.itcast.user.controller;

import cn.itcast.user.config.ConfigProperties;
import cn.itcast.user.entity.TbUser;
import cn.itcast.user.service.TbUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
//@RefreshScope  // 使用 @Value 注解, 需要使用该开启配置热更新
@RestController
@RequestMapping("/user")
public class TbUserController {

    @Autowired
    private TbUserService userService;

//    @Value("${pattern.dateformat}")
//    private String dateformat;

    @Autowired
    private ConfigProperties patternProperties;

    @GetMapping("/now")
    public String now() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateformat()));
    }

    @GetMapping("/prop")
    public ConfigProperties prop() {
        return patternProperties;
    }


    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public TbUser queryById(@PathVariable("id") Long id,
                            @RequestHeader(value = "username",required = false) String username,
                            @RequestParam(value = "password", required = false) String password) {
        // @RequestHeader(value = "username",required = false) 会自动获取 Header 的信息
        if (StringUtils.hasLength(username)) {
            System.out.println("username: " + username);
        }
        if (StringUtils.hasLength(password)) {
            System.out.println("password: " + password);
        }
        return userService.getById(id);
    }
}
